package com.tzbank.face.utils;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Base64;

import com.alibaba.fastjson.JSON;

import com.tzbank.utils.DateUtils;

/**
 * 工具类
 * 
 * @author wangdajun&llz
 *
 */
public class AppUtils {
	/**
	 * 建立连接
	 * 
	 * @return
	 */
	// public static AntCloudProdClient antCloudProdClient () {
	//
	// AntCloudProdClient antCloudClient =
	// AntCloudProdClient.newBuilder().setEndpoint(AppValue.getENDPOINT()).setAccess(AppValue.getACCESS_KEY(),
	// AppValue.getACCESS_SECRET()) .build();
	//
	// return antCloudClient;
	// }

	/**
	 * 接入方请求的唯一标识，由字母、数字、下划线组成，商户须确保其唯一性。 （建议：开头使用一段自定义商户简称，中间使用一段日期，末尾使用一个序列）
	 * 
	 * @return
	 */
	public static String getbizId() {
		String tzb = "TZBANK";
		String time = DateUtils.dateToDateFullString(new Date());
		String uuid = UUID.randomUUID().toString().replace("-", "").substring(10, 20);
		String bizId = tzb + "-" + uuid + "-" + time;
		return bizId;
	}

	/**
	 * 公钥
	 */
	private static String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxav0oL0tJJf1KxiM1rDBChK/ANM7yWkA/sjhZwfZRjPpbGuMLN2HDtfyyYKtj5jg8AggarvofiVmC0omyWiiZUeZ+z9pjH+SxgFbrf6rHPNNkg7Q6S8WgBUOMK+nyus3u2U7YkxJgk/qtGNM72VP8x2TJgGavZfxH1rfFdf041qVGxSSZBgtU+ic5TX31XbIeKCd+BBy+AZiBTnqE19rbSfqNebHRAQzOfwUBiMbvmnQ4FagH6cqLPyH+NMQm5fWHPkCdgNgf8sp+5Du7kgmUkLljxz2Zqg3fQlYSrCjSrmQs2brIuoMTeC91wWNjxyS56vcLRAjMUZsIGqPNHlScwIDAQAB";

	public static String toRSA(Map<String, String> map) {

		/**
		 * 用户信息
		 */
		/*
		 * Map<String, String> identityParam = new HashMap<String, String>();
		 * identityParam.put("identityType", "CERT");//保持不变
		 * identityParam.put("certType", "IDCARD");//保持不变 identityParam.put("certName",
		 * "汪达俊");//用户姓名 identityParam.put("certNo", "340521199311142312");//用户身份证号
		 */
		byte[] publicKeyDecode = Base64.decodeBase64(publicKey);
		byte[] rsaCryptoBytes = null;
		try {
			/**
			 * RSA加密
			 */
			rsaCryptoBytes = RSACryptoUtil.rsaCrypto((JSON.toJSONString(map)).getBytes("UTF-8"), publicKeyDecode, "RSA",
					Cipher.ENCRYPT_MODE);

		} catch (UnsupportedEncodingException e) {
			System.out.println("转码异常:" + e);
		} catch (GeneralSecurityException e) {
			System.out.println("加密异常:" + e);
		}
		String identityParamEncrypt = "";
		if (rsaCryptoBytes != null) {
			identityParamEncrypt = Base64.encodeBase64URLSafeString(rsaCryptoBytes);
		}

		return identityParamEncrypt;
	}

}
